package com.yonyou.cyxdms.basedata.service.service.feign;

import com.xiaoleilu.hutool.date.DateTime;
import com.xiaoleilu.hutool.date.DateUtil;
import com.yonyou.cyxdms.basedata.service.dto.VsProductDTO;
import com.yonyou.cyxdms.basedata.service.feign.CxfClient;
import com.yonyou.cyxdms.basedata.service.service.product.VsProductService;
import com.yonyou.cyxdms.basedata.service.vo.TiProductPriceDataVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class SendVehiclePriceServiceImpl implements SendVehiclePriceService{
    @Resource
    private CxfClient cxfClient;

    @Autowired
    private VsProductService vsProductService;

    @Override
    public void sendVehiclePriceToDms() {
        HashMap<String, String> map = createParam();
        log.info("产品主数据下发查询IN{}",map);
        Map partPriceInfo = (Map) cxfClient.getPartPriceInfo(map).get("data");
        log.info("产品主数据下发返回OUT{}",partPriceInfo);
        updatePrice(map, partPriceInfo);
    }

    @Override
    public void updatePrice() {
        log.info("产品主数据截止时间更新（晚上定时更新）");
        List<TiProductPriceDataVO> list2 = vsProductService.queryProductPriceLog(1);
        if (CollectionUtils.isNotEmpty(list2)) {
            for (TiProductPriceDataVO vo : list2) {
                //更新物料表
                VsProductDTO dto = new VsProductDTO();
                dto.setProductCode(vo.getProductCode());
                dto.setOemDirectivePrice(null);
                log.info("更新车辆主数据IN{}", dto);
                vsProductService.updateProductPrice(dto);
                vo.setBack2("2");
                vsProductService.updateProductPriceLog(vo);
            }
        }

        log.info("产品主数据更新开始时间更新（凌晨定时更新）");
        List<TiProductPriceDataVO> list = vsProductService.queryProductPriceLog(0);
        if (CollectionUtils.isNotEmpty(list)) {
            for (TiProductPriceDataVO vo : list) {
                //更新物料表
                VsProductDTO dto = new VsProductDTO();
                dto.setProductCode(vo.getProductCode());
                dto.setOemDirectivePrice(Double.valueOf(vo.getBasicPrice()));
                log.info("更新车辆主数据IN{}", dto);
                vsProductService.updateProductPrice(dto);
                vo.setBack2("1");
                vsProductService.updateProductPriceLog(vo);
            }
        }

    }

    private void updatePrice(HashMap<String, String> map, Map partPriceInfo) {
        List<Map> tbOut = (List<Map>) partPriceInfo.get("tbOut");
        List<Map> tbDetail = (List<Map>) partPriceInfo.get("tbDetail");
        if(CollectionUtils.isNotEmpty(tbOut)){
            Map<String,String> map1 = tbOut.get(0);
            String rowId = map1.get("ROW_ID");
            if(CollectionUtils.isNotEmpty(tbDetail)){
                for (Map map2 : tbDetail) {
                    //插入接口表
                    TiProductPriceDataVO vo = new TiProductPriceDataVO();
                    vo.setSender("sap");
                    vo.setReceiver("basedate");
                    vo.setDtsend(new Date());
                    vo.setIntfid("03200000000000");
                    vo.setMsgid(rowId);
                    vo.setProductCode((String) map2.get("MATNR"));
                    vo.setBasicPrice((String)(map2.get("KBETR")));
                    vo.setCurrency((String) map2.get("KONWA"));
                    String datab = (String) map2.get("DATAB");
                    String datbi = (String) map2.get("DATBI");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date parse = null;
                    Date parse1 = null;
                    try {
                        parse = sdf.parse(datab + " 00:00:00");
                        parse1 = sdf.parse(datbi + " 00:00:00");
                    } catch (ParseException e) {
                        log.info("格式化日期错误");
                    }
                    vo.setStartDate(parse);
                    vo.setEndDate(parse1);
                    vo.setBack1((String) map2.get("LOEVM_KO"));//传输空值表示价格有效，传输X表示价格被删除
                    vo.setBack2("0");//表示未处理
                    log.info("插入接口表IN{}",vo);
                    vsProductService.sapInsertInterface(vo);
                }
            }
        }
    }


    private HashMap<String, String> createParam() {
        HashMap<String, String> map = new HashMap<>();
        String rowId = UUID.randomUUID().toString();
        String yesterday = DateUtil.format(DateUtil.yesterday(), "yyyyMMdd");
        String today = DateUtil.format(new DateTime(), "yyyyMMdd");
        map.put("ROW_ID", rowId);
        /*ZB11-整车市场指导价
          ZB21-备件服务站订购价
          ZB22-备件终端销售价*/
        map.put("KSCHL", "ZB11");
        map.put("VKORG", "3723");
        map.put("ZQSRQ", yesterday);
        map.put("ZZZRQ", today);
//        map.put("ZQSSJ","000000");
//        map.put("ZZZSJ","235959");
        return map;
    }
}
